package ar.com.braghini.analisis.lenguajes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;

import org.apache.commons.io.IOUtils;

public class VentanaController implements Initializable {

	@FXML
	private Button explorarBtn;

	@FXML
	private TextField filePathTxtField;

	@FXML
	private Button graficarBtn;

	@FXML
	private LineChart<String, Number> lineChart;

	@FXML
	private CategoryAxis categoryAxis;

	@FXML
	private NumberAxis numberAxis;

	public void initialize(URL arg0, ResourceBundle arg1) {
		numberAxis.setLabel("Apariciones");
		categoryAxis.setLabel("Palabra");
		explorarBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent arg0) {
				FileChooser fileChooser = new FileChooser();

				// Show open file dialog
				File file = fileChooser.showOpenDialog(null);

				filePathTxtField.setText(file.getPath());

			}
		});

		graficarBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent arg0) {
				graficar();
			}
		});

	}

	protected void graficar() {
		FileInputStream inputStream = null;
		try {
			String todoElTexto = "";
			inputStream = new FileInputStream(filePathTxtField.getText());
			todoElTexto = IOUtils.toString(inputStream);
			inputStream.close();
			SeparadorEnPalabras separador = new SeparadorEnPalabras();
			AparicionesCounter aparicionesCounter = new AparicionesCounter();
			aparicionesCounter.computarApariciones(separador
					.separarEnPalabras(todoElTexto));
			crearGrafico(aparicionesCounter.getDiccionario());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void crearGrafico(HashMap<String, Integer> diccionario) {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		ValueComparator bvc = new ValueComparator(diccionario);
		TreeMap<String, Integer> mapValores = new TreeMap<String, Integer>(bvc);

		mapValores.putAll(diccionario);

		XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
		series.setName("Lenguaje");

		for (String clave : mapValores.keySet()) {
			series.getData().add(
					new XYChart.Data<String, Number>(clave, diccionario
							.get(clave)));
			System.out.println(clave + " : " + diccionario.get(clave));
			;

		}
		try {
			lineChart.getData().get(0);
			lineChart.getData().remove(0);
		} catch (IndexOutOfBoundsException e) {
			// IGNORADA
		}
		lineChart.getData().add(series);
		lineChart.setVisible(false);
		lineChart.setVisible(true);
		System.out.println("Completado!");
	}
}

class ValueComparator implements Comparator<String> {

	Map<String, Integer> base;

	public ValueComparator(Map<String, Integer> base) {
		this.base = base;
	}

	// Note: this comparator imposes orderings that are inconsistent with
	// equals.
	public int compare(String a, String b) {
		if (base.get(a) >= base.get(b)) {
			return -1;
		} else {
			return 1;
		} // returning 0 would merge keys
	}
}
